import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: (resolve) => require(['@/views/login/index'], resolve),
    hidden: true
  },

  {
    path: '/register',
    component: (resolve) => require(['@/views/register/index'], resolve),
    hidden: true
  },

  {
    path: '/progress/:project_id',
    component: (resolve) => require(['@/views/details/index'], resolve),
    hidden: true
  },

  {
    path: '',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: (resolve) => require(['@/views/dashboard/index'], resolve),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  {
    path: '/system',
    component: Layout,
    redirect: '/system/project',
    name: 'System',
    meta: { title: '系统功能', icon: 'el-icon-s-tools' },
    children: [
      {
        path: 'project',
        name: 'Project',
        component: (resolve) => require(['@/views/project/index'], resolve),
        meta: { title: '项目管理', icon: 'table' }
      },
      {
        path: 'progress',
        name: 'Progress',
        component: (resolve) => require(['@/views/progress/index'], resolve),
        meta: { title: '项目进度', icon: 'progress' }
      }
    ]
  },

  {
    path: '/user',
    component: Layout,
    children: [
      {
        path: 'profile',
        component: (resolve) => require(['@/views/user/index'], resolve),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  },

  {
    path: 'swagger',
    component: Layout,
    children: [
      {
        path: 'http://localhost:8080/swagger-ui/index.html',
        meta: { title: '接口文档', icon: 'el-icon-s-promotion' }
      }
    ]
  },

  {
    path: 'druid',
    component: Layout,
    children: [
      {
        path: 'http://localhost:8080/druid/',
        meta: { title: '数据库监控', icon: 'link' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
